package com.rongshu.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.ColorInt;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.View;

import com.rongshu.R;
import com.rongshu.utils.clickableSpan.SpanableClickable;
import com.rongshu.utils.clickableSpan.UnderLineClickSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpannableStringUtils {
    //正则
    private static String REGEX = "@[\\u4e00-\\u9fa5\\w\\-\\_]+ ";
    private static Pattern pattern = null;
    private static Matcher matcher = null;

    /**
     * 1种文字颜色
     */
    public static SpannableString addForeColorSpan(String content, @ColorInt int color, int start, View.OnClickListener onClickListener) {
        SpannableString spanString = new SpannableString(content);
        ForegroundColorSpan span = new ForegroundColorSpan(color);
        spanString.setSpan(span, start, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        UnderLineClickSpan underLineClickSpan = new UnderLineClickSpan();
        underLineClickSpan.setOnClickListener(onClickListener, color);

        spanString.setSpan(underLineClickSpan, start, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    /**
     * 多种文字颜色
     */
    public static SpannableString addMoreForeColorSpan(String content, @ColorInt int f_color, int f_start, int f_end, View.OnClickListener f_onClilkListener, int s_start, int s_end, View.OnClickListener s_onClilkListener) {
        SpannableString spanString = new SpannableString(content);

        ForegroundColorSpan f_span = new ForegroundColorSpan(f_color);
        spanString.setSpan(f_span, f_start, f_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ForegroundColorSpan s_span = new ForegroundColorSpan(f_color);
        spanString.setSpan(s_span, s_start, s_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        UnderLineClickSpan f_click = new UnderLineClickSpan();
        f_click.setOnClickListener(f_onClilkListener, f_color);
        spanString.setSpan(f_click, f_start, f_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        f_click = new UnderLineClickSpan();
        f_click.setOnClickListener(s_onClilkListener, f_color);
        spanString.setSpan(f_click, s_start, s_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spanString;
    }

    /**
     * 文字颜色
     */
    public static SpannableString addForeColorSpan(String content, @ColorInt int color, int start, int end, View.OnClickListener onClickListener) {
        SpannableString spanString = new SpannableString(content);
        ForegroundColorSpan span = new ForegroundColorSpan(color);
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        UnderLineClickSpan underLineClickSpan = new UnderLineClickSpan();
        underLineClickSpan.setOnClickListener(onClickListener, color);

        spanString.setSpan(underLineClickSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    /**
     * 带点击背景
     */
    public static SpannableStringBuilder addBackGroundColorClickSpan(String content, SpanableClickable spanableClickable) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        SpannableString spanableString = new SpannableString(content);
        spanableString.setSpan(spanableClickable, 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(spanableString);
        return builder;
    }

    /**
     * 处理@点击
     */
    public static SpannableStringBuilder dealwithContent(Context context, String content) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //正则
        pattern = Pattern.compile(REGEX);
        matcher = pattern.matcher(builder.toString());
        while (matcher.find()) {
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_3cc051)),
                    matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }


    /**
     * 修改字体颜色和大小
     */
    public static SpannableStringBuilder addAbsoluteSizeSpan(String content, int color, int start, int end) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //字体大小
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(14, true);
        //文本字体绝对的大小
        builder.setSpan(sizeSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //文字颜色
        ForegroundColorSpan span = new ForegroundColorSpan(color);
        builder.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }


    /**
     * 修改字体颜色和大小
     */
    public static SpannableStringBuilder addAbsoluteSizeSpans(String content, int color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //文字颜色
        builder.setSpan(new StyleSpan(Typeface.BOLD), content.indexOf("数") + 1, content.indexOf("阅读"), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(color), content.indexOf("数") + 1, content.indexOf("阅读"), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        builder.setSpan(new StyleSpan(Typeface.BOLD), content.indexOf("度") + 1, content.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(color), content.indexOf("度") + 1, content.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        //字体大小
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(14, true);
        //文本字体绝对的大小
        builder.setSpan(sizeSpan, content.indexOf("需") + 1, content.indexOf("min"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new StyleSpan(Typeface.BOLD), content.indexOf("需") + 1, content.indexOf("min"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(color), content.indexOf("需") + 1, content.indexOf("min"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }


    /**
     * 添加下划线
     */
    public static SpannableStringBuilder addUnderLineSpan(String content, int start, int end) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //下划线
        builder.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }
}
